package business_opportunity

import (
	"context"
	"encoding/json"
	"errors"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/errcode"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	businessOpportunityCustomer "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/business_opportunity/customer"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/job_center"
	"git.myscrm.cn/golang/stark/v4"
)

type customerServer struct {
	businessCustomerService businessOpportunityCustomer.CustomerServiceIface
	jobService              job_center.JobCenterServiceIface
}

func NewCustomerServer(
	businessCustomerService businessOpportunityCustomer.CustomerServiceIface,
	jobService job_center.JobCenterServiceIface,
) pb.CustomerServiceServer {
	return &customerServer{
		businessCustomerService: businessCustomerService,
		jobService:              jobService,
	}
}

// GetCustomerScore 获取好客户得分
func (s *customerServer) GetCustomerScore(ctx context.Context, request *pb.GetCustomerScoreRequest) (*pb.GetCustomerScoreResponse, error) {
	resp, err := s.businessCustomerService.GetCustomerScore(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetCustomerScore request:%+v, err:%s", request, err)
		return nil, err
	}
	if len(resp) == 0 {
		return &pb.GetCustomerScoreResponse{}, nil
	}
	stark.Logger.Infof(ctx, "grpc GetCustomerScore request:%+v, response:%#v", request, resp)
	return resp[0], nil
}

// GetCustomerCommencePermit 获取客户详情的开工数据
func (s *customerServer) GetCustomerCommencePermit(ctx context.Context, request *pb.GetCustomerCommencePermitRequest) (*pb.GetCustomerCommencePermitResponse, error) {
	resp, err := s.businessCustomerService.GetCustomerCommencePermitList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetCustomerCommencePermit request:%+v, err:%s", request, err)
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc GetCustomerCommencePermit request:%+v, response:%+v", request, resp)
	return resp, nil
}

// GetSupplierProject 供应商合作案例列表
func (s *customerServer) GetSupplierProject(ctx context.Context, request *pb.GetSupplierProjectRequest) (*pb.GetSupplierProjectResponse, error) {
	var errMsg string
	if request.GetOrganizingCode() == "" {
		errMsg = "organizing_code参数不能为空"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	resp, err := s.businessCustomerService.GetSupplierProject(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetSupplierProject request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

// GetFeed 客户动态
func (s *customerServer) GetFeed(ctx context.Context, request *pb.GetFeedRequest) (*pb.GetFeedResponse, error) {
	var errMsg string
	if request.GetOrganizingCode() == "" {
		errMsg = "organizing_code参数不能为空"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	resp, err := s.businessCustomerService.GetFeed(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetFeed request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

// GetInvestCompany 投资企业
func (s *customerServer) GetInvestCompany(ctx context.Context, request *pb.GetInvestCompanyRequest) (*pb.GetInvestCompanyResponse, error) {
	var errMsg string
	if request.GetOrganizingCode() == "" {
		errMsg = "organizing_code参数不能为空"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	resp, err := s.businessCustomerService.GetInvestCompany(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetInvestCompany request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *customerServer) SearchCustomerList(ctx context.Context, request *pb.SearchCustomerListRequest) (*pb.SearchCustomerListResponse, error) {
	resp, err := s.businessCustomerService.SearchCustomerList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc SearchCustomerList request:%+v, err:%s", request, err)
		return nil, err
	}
	return resp, nil
}

func (s *customerServer) GetCustomerDetail(ctx context.Context, request *pb.GetCustomerDetailRequest) (*pb.GetCustomerDetailResponse, error) {
	resp, err := s.businessCustomerService.GetCustomerDetail(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		if grpcErr.GetCode() != errcode.CustomerNotExistError {
			stark.Logger.Errorf(ctx, "grpc GetCustomerDetail request:%+v, err:%s", request, err)
		}
		return nil, err
	}
	return resp, nil
}

func (s *customerServer) GetCustomerZxList(ctx context.Context, request *pb.GetCustomerRiskCommonRequest) (*pb.GetCustomerZxListResponse, error) {
	resp, err := s.businessCustomerService.GetCustomerZxList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetCustomerZxList request:%+v, err:%s", request, err)
		return nil, err
	}
	return resp, nil
}

func (s *customerServer) GetCustomerCourtRegister(ctx context.Context, request *pb.GetCustomerRiskCommonRequest) (*pb.GetCustomerCourtRegisterResponse, error) {
	resp, err := s.businessCustomerService.GetCustomerCourtRegister(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetCustomerCourtRegister request:%+v, err:%s", request, err)
		return nil, err
	}
	return resp, nil
}

func (s *customerServer) GetCustomerLawSuit(ctx context.Context, request *pb.GetCustomerRiskCommonRequest) (*pb.GetCustomerLawSuitResponse, error) {
	resp, err := s.businessCustomerService.GetCustomerLawSuit(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetCustomerLawSuit request:%+v, err:%s", request, err)
		return nil, err
	}
	return resp, nil
}

func (s *customerServer) GetCustomerTaxIllegal(ctx context.Context, request *pb.GetCustomerRiskCommonRequest) (*pb.GetCustomerTaxIllegalResponse, error) {
	resp, err := s.businessCustomerService.GetCustomerTaxIllegal(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetCustomerTaxIllegal request:%+v, err:%s", request, err)
		return nil, err
	}
	return resp, nil
}

func (s *customerServer) FuzzySearchCustomer(ctx context.Context, request *pb.FuzzySearchCustomerRequest) (*pb.FuzzySearchCustomerResponse, error) {
	resp, err := s.businessCustomerService.FuzzySearch(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc FuzzySearchCustomer request:%+v, err:%s", request, err)
		return nil, err
	}
	return resp, nil
}

func (s *customerServer) GetCustomerRiskCount(ctx context.Context, request *pb.GetCustomerRiskCountRequest) (*pb.GetCustomerRiskCountResponse, error) {
	resp, err := s.businessCustomerService.GetCustomerRiskCount(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetCustomerRiskCount request:%+v, err:%s", request, err)
		return nil, err
	}
	return resp, nil
}

func (s *customerServer) ExportCustomerList(ctx context.Context, request *pb.SearchCustomerListRequest) (*pb.ExportCustomerListResponse, error) {
	request.Page = 1
	request.PageSize = 1
	listResp, err := s.businessCustomerService.SearchCustomerList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ExportCustomerList request:%+v, err:%s", request, err)
		return nil, err
	}
	if listResp.Total == 0 {
		return nil, errors.New("导出数据为空")
	}
	if listResp.Total > 10000 {
		listResp.Total = 10000
	}
	requestBytes, _ := json.Marshal(request)

	jobId, err := s.jobService.NewJob(ctx, constants.JobMethodExport, constants.JobTypeMarketCustomerExport, listResp.Total, string(requestBytes))
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ExportBuildingList NewJob request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	_ = s.jobService.StartJob(ctx, jobId)

	return &pb.ExportCustomerListResponse{
		JobId: jobId,
	}, nil
}
